package cn.mcch.investigator.customview;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.mcch.investigator.listener.OnCommonClickListener;
import cn.mcch.investigator.utils.DensityUtils;

/**
 * 自定义title bar
 * 
 * @author Administrator
 *
 */
public class TitleBar extends RelativeLayout {

	private TextView tvTitle;
	private RelativeLayout rlLeft;
	private RelativeLayout rlRight;
	private ImageView ivLeft;
	private ImageView ivRight;

	public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	public TitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public TitleBar(Context context) {
		super(context);
		init(context);
	}

	/**
	 * 初始化
	 * 
	 * @param context
	 */
	private void init(Context context) {
		tvTitle = new TextView(context);
		tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		tvTitle.setTextColor(Color.WHITE);
		tvTitle.setSingleLine();
		rlLeft = new RelativeLayout(context);
		rlLeft.setId(getResources().getIdentifier("rl_titlebar_left", "id", context.getPackageName()));
		rlRight = new RelativeLayout(context);
		rlRight.setId(getResources().getIdentifier("rl_titlebar_right", "id", context.getPackageName()));
		ivLeft = new ImageView(context);
		ivRight = new ImageView(context);

		// 标题
		RelativeLayout.LayoutParams tvTitleLayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		tvTitleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		tvTitle.setLayoutParams(tvTitleLayoutParams);
		this.addView(tvTitle);
		// 左边
		RelativeLayout.LayoutParams rlLeftLayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		rlLeftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		rlLeft.setLayoutParams(rlLeftLayoutParams);
		this.addView(rlLeft);
		RelativeLayout.LayoutParams ivLeftLayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		ivLeftLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
		ivLeftLayoutParams.leftMargin = DensityUtils.dp2px(context, 15);
		ivLeftLayoutParams.rightMargin = DensityUtils.dp2px(context, 15);
		ivLeft.setLayoutParams(ivLeftLayoutParams);
		rlLeft.addView(ivLeft);
		// 右边
		RelativeLayout.LayoutParams rlRightLayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		rlRightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		rlRight.setLayoutParams(rlRightLayoutParams);
		this.addView(rlRight);
		RelativeLayout.LayoutParams ivRightLayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		ivRightLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
		ivRightLayoutParams.leftMargin = DensityUtils.dp2px(context, 15);
		ivRightLayoutParams.rightMargin = DensityUtils.dp2px(context, 15);
		ivRight.setLayoutParams(ivRightLayoutParams);
		rlRight.addView(ivRight);
	}

	/**
	 * 设置titlebar相关属性
	 * 
	 * @param title
	 * @param ivLeftId
	 * @param ivRightId
	 * @param l
	 */
	public void initTitleBar(String title, int ivLeftId, int ivRightId, OnCommonClickListener l) {

		// 设置title
		tvTitle.setVisibility(!TextUtils.isEmpty(title) ? View.VISIBLE : View.GONE);
		if (!TextUtils.isEmpty(title)) {
			tvTitle.setText(title);
		}

		// 左边
		rlLeft.setVisibility(ivLeftId == 0 ? View.GONE : View.VISIBLE);
		if (ivLeftId != 0) {
			ivLeft.setBackgroundResource(ivLeftId);
			if (l != null) {
				rlLeft.setOnClickListener(l);
			}
		}

		// 右边
		rlRight.setVisibility(ivRightId == 0 ? View.GONE : View.VISIBLE);
		if (ivRightId != 0) {
			ivRight.setBackgroundResource(ivRightId);
			if (l != null) {
				rlRight.setOnClickListener(l);
			}
		}

	}

	/**
	 * 设置titlebar相关属性
	 * 
	 * @param title
	 * @param titleTextColor
	 * @param titleTextSize
	 * @param ivLeftId
	 * @param ivRightId
	 * @param l
	 */
	public void initTitleBar2(String title, int titleTextColor, float titleTextSize, int ivLeftId, int ivRightId,
			OnCommonClickListener l) {

		initTitleBar(title, ivLeftId, ivRightId, l);
		tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, titleTextSize);
		tvTitle.setTextColor(titleTextColor);
	}

	public void setTitleBarText(String title){
		tvTitle.setText(title);
	}
}
